package servlet;

import biz.AdminService;
import pojo.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet(name ="LoginServlet",urlPatterns ="/LoginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name  = request.getParameter("username");
            String pwd = request.getParameter("password");
            Admin a = new Admin(name,pwd);
            AdminService adminService = new AdminService();
            Admin admin = adminService.loginAdmin(a);
            if(admin !=null){
                HttpSession session =  request.getSession();
                session.setAttribute("admin", admin);
                request.getRequestDispatcher("homepage.html").forward(request,response);
            }else {
                response.sendRedirect("login.html");
                System.out.println("登录错误！");
            }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
